/*global pi, ArrayBuffer */
pi.mod.define('pi.file', function (pi) {
	"use strict";
	var indexIsFileEqual, indexIsFileExist, indexIsDirExist, indexFileMoveFileEx, indexFileDeleteFile, indexWriteFile, indexReadFile, indexReadBinFile, indexFileGetCrc32, indexFileGetInfo, indexJSFileMD5, indexGetDirectory, indexFileCteateDirRecur, indexFileList, module = {},
		dllPath = "TestDll.dll";

	/*
	 * 判断文件是否与指定的size&crc32相符
	 * file: 文件路径
	 * size: 大小, int
	 * crc32: int
	 */
	indexIsFileEqual = 0;
	module.equals = function (file, size, crc32) {
		if (!indexIsFileEqual) {
			indexIsFileEqual = pi.vcall.register(dllPath, "IsFileEqual", pi.vcall.TYPE_BOOL, [pi.vcall.TYPE_STRING, pi.vcall.TYPE_INT, pi.vcall.TYPE_INT]);
		}
		return pi.vcall.invoke(indexIsFileEqual, file, size, crc32);
	};

	/*
	 * 判断文件是否存在
	 * file: 文件路径
	 */
	indexIsFileExist = 0;
	module.exists = function (file) {
		if (indexIsFileExist === 0) {
			indexIsFileExist = pi.vcall.register(dllPath, "IsFileExist", pi.vcall.TYPE_BOOL, [pi.vcall.TYPE_STRING]);
		}
		return pi.vcall.invoke(indexIsFileExist, file);
	};

	/*
	 * 判断文件夹是否存在
	 * file: 文件夹路径
	 */
	indexIsDirExist = 0;
	module.dirExists = function (dir) {
		if (!indexIsDirExist) {
			indexIsDirExist = pi.vcall.register(dllPath, "IsDirExist", pi.vcall.TYPE_BOOL, [pi.vcall.TYPE_STRING]);
		}
		return pi.vcall.invoke(indexIsDirExist, dir);
	};

	/*
	 * 移动文件
	 * oldName: 老的文件路径
	 * newName: 新的文件路径
	 * return: 是否成功
	 */
	indexFileMoveFileEx = 0;
	module.moveFile = function (oldName, newName) {
		if (!indexFileMoveFileEx) {
			indexFileMoveFileEx = pi.vcall.register(dllPath, "FileMoveFileEx", pi.vcall.TYPE_BOOL, [pi.vcall.TYPE_STRING, pi.vcall.TYPE_STRING]);
		}
		return pi.vcall.invoke(indexFileMoveFileEx, oldName, newName);
	};

	/*
	 * 删除文件
	 * file: 文件路径
	 * return: 是否成功
	 */
	indexFileDeleteFile = 0;
	module.deleteFile = function (file) {
		if (!indexFileDeleteFile) {
			indexFileDeleteFile = pi.vcall.register(dllPath, "FileDeleteFile", pi.vcall.TYPE_BOOL, [pi.vcall.TYPE_STRING]);
		}
		return pi.vcall.invoke(indexFileDeleteFile, file);
	};

	/*
	 * 写文件(覆盖)
	 * path:输出路径
	 * content: 字符串内容
	 * encoding: 编码格式: "utf8", "gbk"(default,传null或者空字符串)
	 */
	indexWriteFile = 0;
	module.write = function (path, content, encoding) {
		if (!indexWriteFile) {
			indexWriteFile = pi.vcall.register(dllPath, "FileWriteFile", pi.vcall.TYPE_INT, [pi.vcall.TYPE_STRING, pi.vcall.TYPE_STRING, pi.vcall.TYPE_STRING]);
		}
		return pi.vcall.invoke(indexWriteFile, path, content, encoding);
	};

	/*
	 * 读文件
	 * path:输入路径
	 * encoding: 编码格式: "utf8", "gbk"(default)
	 * return: 字符串内容
	 */
	indexReadFile = 0;
	module.read = function (path, encoding) {
		if (!indexReadFile) {
			indexReadFile = pi.vcall.register(dllPath, "FileReadFile", pi.vcall.TYPE_STRING, [pi.vcall.TYPE_STRING, pi.vcall.TYPE_STRING]);
		}
		return pi.vcall.invoke(indexReadFile, path, encoding);
	};

	/*
	 * 读文件(二进制)
	 * path:输入路径
	 * buf: ArrayBuffer等缓冲区
	 * [bufLen = -1]buf len, -1则不校验
	 * [offset = 0]: 读取偏移量
	 * [len = -1]: 读取长度,-1则读取bufLen的长度(不能同时为负数)
	 * return: 实际读取长度
	 */
	indexReadBinFile = 0;
	module.readBinary = function (path, buf, bufLen, offset, len) {
		var r;
		if (!indexReadBinFile) {
			indexReadBinFile = pi.vcall.register(dllPath, "FileReadBinFile", pi.vcall.TYPE_INT, [pi.vcall.TYPE_STRING, pi.vcall.TYPE_POINTER, pi.vcall.TYPE_UINT, pi.vcall.TYPE_UINT, pi.vcall.TYPE_UINT]);
		}
		bufLen = pi.util.DV(bufLen, buf.byteLength);
		offset = pi.util.DV(offset, 0);
		len = pi.util.DV(len, -1);
		r = pi.vcall.invoke(indexReadBinFile, path, buf, bufLen, offset, len);
		return r;
	};

	//一次性读取所有的内容
	//return: ArrayBuffer
	module.readBinaryAll = function (path) {
		var buf, read = 0,
			info = {};
		if (pi.file.getInfo(info, path)) {
			buf = new ArrayBuffer(info.size);
			read = pi.file.readBinary(path, buf);
			if (read != info.size) {
				buf = undefined;
				pi.log("pi.file.readBinaryAll error, size=" + info.size + ", actual read=" + read);
			}
		} else {
			pi.log("pi.file.readBinaryAll error, get info fail");
		}
		return buf;
	};

	/*
	 * 获取文件列表
	 * obj:[out, Array], 存放所有的文件名
	 * dir:[String], 根目录
	 * bRecure:[bool], 是否递归
	 * filter:[String], 要包含的文件类型过滤字符串."." or "*.*" 表示匹配所有文件,".txt"与"*.txt"等价,用';'分隔
	 */
	indexFileList = 0;
	module.list = function (arr, dir, bRecur, filter) {
		if (!indexFileList) {
			indexFileList = pi.vcall.register(dllPath, "JSFileList", pi.vcall.TYPE_VOID, [pi.vcall.TYPE_OBJECT, pi.vcall.TYPE_STRING, pi.vcall.TYPE_BOOL, pi.vcall.TYPE_STRING]);
		}
		return pi.vcall.invoke(indexFileList, arr, dir, bRecur, filter);
	};

	/*
	 * 获取文件crc32值
	 * crcObj:[out, Object], crcObj.value为crc32值
	 * file:[String], 指定文件路径
	 * return: 返回1表示成功，0表示没有找到该文件，-1表示该文件读取数据失败
	 */
	indexFileGetCrc32 = 0;
	module.getCrc32 = function (crcObj, file) {
		if (!indexFileGetCrc32) {
			indexFileGetCrc32 = pi.vcall.register(dllPath, "JSFileGetCrc32", pi.vcall.TYPE_INT, [pi.vcall.TYPE_OBJECT, pi.vcall.TYPE_STRING]);
		}
		return pi.vcall.invoke(indexFileGetCrc32, crcObj, file);
	};

	/*
	 * 获取文件信息
	 * infoObj:[out, Object], 返回的信息有:
	 *         int attrib;//属性
	 *         double createTime;    -1 for FAT file systems
	 *         double accessTime;    -1 for FAT file systems
	 *         double writeTime; 修改时间
	 *         double size;//大小
	 *         string name;//文件名
	 * file:[String], 指定文件路径
	 * return: 返回true表示获取信息成功
	 */
	indexFileGetInfo = 0;
	module.getInfo = function (infoObj, file) {
		if (!indexFileGetInfo) {
			indexFileGetInfo = pi.vcall.register(dllPath, "JSFileGetInfo", pi.vcall.TYPE_BOOL, [pi.vcall.TYPE_OBJECT, pi.vcall.TYPE_STRING]);
		}
		return pi.vcall.invoke(indexFileGetInfo, infoObj, file);
	};

	//获取文件md5值
	indexJSFileMD5 = 0;
	module.md5 = function (path) {
		if (!indexJSFileMD5) {
			indexJSFileMD5 = pi.vcall.register(dllPath, "JSFileMD5", pi.vcall.TYPE_STRING, [pi.vcall.TYPE_STRING]);
		}
		return pi.vcall.invoke(indexJSFileMD5, path);
	};

	//获取文件(夹)的parent路径
	indexGetDirectory = 0;
	module.getDir = function (path) {
		if (!indexGetDirectory) {
			indexGetDirectory = pi.vcall.register(dllPath, "JSGetDirectory", pi.vcall.TYPE_STRING, [pi.vcall.TYPE_STRING]);
		}
		return pi.vcall.invoke(indexGetDirectory, path);
	};
	
	//递归创建文件夹的路径
	indexFileCteateDirRecur = 0;
	module.createDir = function (path) {
		if (!indexFileCteateDirRecur) {
			indexFileCteateDirRecur = pi.vcall.register(dllPath, "FileCteateDirRecur", pi.vcall.TYPE_INT, [pi.vcall.TYPE_STRING]);
		}
		return pi.vcall.invoke(indexFileCteateDirRecur, path);
	};

	//用中文对应的字符代替非法字符.
	module.validateName = function (name) {
		name = name.replace(/\\/g, "＼");
		name = name.replace(/\//g, "／");
		name = name.replace(/:/g, "：");
		name = name.replace(/\*/g, "＊");
		name = name.replace(/\?/g, "？");
		name = name.replace(/"/g, "＂");
		name = name.replace(/</g, "＜");
		name = name.replace(/>/g, "＞");
		name = name.replace(/\|/g, "｜");
		return name;
	};

	return module;
});